﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SQLite;

namespace WowRaidListTracker.DAL
{
    public class RaidMember
    {
        public int Id
        {
            get;
            set;
        }

        public string Name
        {
            get;
            set;
        }

        private bool isPresent;
        public bool IsPresent
        {
            get { return isPresent; }
            set
            {
                isPresent = value;
                UpdatePresence(isPresent);
            }
        }

        public int CharacterClassId
        {
            get;
            set;
        }

        public string CharacterClassName
        {
            get;
            set;
        }

        public int PrimarySpecialization
        {
            get;
            set;
        }

        public int SecondarySpecialization
        {
            get;
            set;
        }

        public int TertiarySpecialization
        {
            get;
            set;
        }

        public RaidMember() { }

        public RaidMember(DataRow dr)
        {
            Load(dr);
        }

        public RaidMember(int id)
        {
            LoadMember(id);
        }

        public void AddMember()
        {
            ListTrackerDal data = new ListTrackerDal();
            CRUDReader qud = new CRUDReader();
            List<SQLiteParameter> parameters = new List<SQLiteParameter>();
            parameters.Add(new SQLiteParameter("@Name", Name));
            parameters.Add(new SQLiteParameter("@Present", IsPresent));
            parameters.Add(new SQLiteParameter("@ClassId", CharacterClassId));
            parameters.Add(new SQLiteParameter("@Primary", PrimarySpecialization));
            parameters.Add(new SQLiteParameter("@Secondary", SecondarySpecialization));
            parameters.Add(new SQLiteParameter("@Tertiary", TertiarySpecialization));
            object dt = data.Execute(qud.get("InsertRaidMember", CRUDReader.QUDType.Insert), SQLiteQueryType.STANDARDQUERY, parameters);
            Id = Convert.ToInt32(((DataTable)dt).Rows[0]["Identity"]);
        }

        public void UpdatePresence(bool isPresent)
        {
            UpdatePresence(Id, isPresent);
        }

        public void UpdatePresence(int memberId, bool isPresent)
        {
            ListTrackerDal dal = new ListTrackerDal();
            List<SQLiteParameter> parameters = new List<SQLiteParameter>();
            parameters.Add(new SQLiteParameter("@Id", memberId));
            parameters.Add(new SQLiteParameter("@Presence", isPresent));
            object dt = dal.Execute(CRUDReader.Crud.get("UpdateRaidMemberPresence", CRUDReader.QUDType.Update), SQLiteQueryType.NONSTANDARDQUERY, parameters);
            NonQueryResult nqr = (dt as NonQueryResult);
        }

        public NonQueryResult RemoveMember()
        {
            return RemoveMember(Id);
        }

        public NonQueryResult RemoveMember(int id)
        {
            ListTrackerDal data = new ListTrackerDal();
            CRUDReader qud = new CRUDReader();
            List<SQLiteParameter> parameters = new List<SQLiteParameter>();
            parameters.Add(new SQLiteParameter("@Id", id));
            NonQueryResult nrq = (NonQueryResult)data.Execute(qud.get("DeleteRaidMember", CRUDReader.QUDType.Delete), SQLiteQueryType.NONSTANDARDQUERY, parameters);
            return nrq;
        }

        public void LoadMember(int id)
        {
            ListTrackerDal data = new ListTrackerDal();
            CRUDReader qud = new CRUDReader();
            List<SQLiteParameter> parameters = new List<SQLiteParameter>();
            parameters.Add(new SQLiteParameter("@Id", id));
            object result = data.Execute(qud.get("RaidMemberById", CRUDReader.QUDType.Select), SQLiteQueryType.STANDARDQUERY, parameters);
            if (typeof(DataTable).Equals(result.GetType()))
            {
                // populate the member values
                DataRow dr = ((DataTable)result).Rows[0];
                Load(dr);
            }
        }

        private void Load(DataRow dr)
        {
            Id = Convert.ToInt32(dr["RaidMemberId"]);
            Name = Convert.ToString(dr["RaidMemberName"]);
            IsPresent = Convert.ToBoolean(dr["RaidMemberIsPresent"]);
            CharacterClassId = Convert.ToInt32(dr["RaidMemberCharacterClassId"]);
            CharacterClassName = Convert.ToString(dr["CharacterClassName"]);
            PrimarySpecialization = Convert.ToInt32(dr["RaidMemberPrimarySpecialization"]);
            SecondarySpecialization = Convert.ToInt32(dr["RaidMemberSecondarySpecialization"]);
            if (dr["RaidMemberTertiarySpecialization"].GetType() == typeof(DBNull))
                TertiarySpecialization = 0;
            else
                TertiarySpecialization = Convert.ToInt32(dr["RaidMemberTertiarySpecialization"]);
        }
    }
}
